package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.SetmealVO;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
/**
 * @Description :移动端套餐管理
 * @author     :LD
 */
@RestController
@RequestMapping("setmeal")
public class SetMealController {
    @Reference(timeout = 1500,mock = "com.itheima.health.mock.SetMealServiceMock")
    private SetMealService setMealService;

    @Reference
    MemberService memberService;

    @GetMapping("findAllSetmeal")
    public Result findAllSetmeal(){
        List<Setmeal> setmealList = setMealService.findAll();
        return new Result(setmealList);
    }

    @GetMapping("findSetMealDetail")
    public Result findSetMealDetail(@RequestParam("id")Integer id){
        Setmeal setmeal = setMealService.findSetMealDetail(id);
        return new Result(setmeal);
    }

    @GetMapping("findSetMealById")
    public Result findSetMealById(@RequestParam("id")Integer id){
        return new Result(setMealService.findSetMealById(id));
    }

//    @GetMapping("demo")
//    public String demo(HttpServletResponse response){
//        Map map = memberService.getDataCount();
//        //在内存中创建一个Excel文件
//        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//        //创建工作表，指定工作表名称
//        XSSFSheet sheet = xssfWorkbook.createSheet("统计分析");
//        //创建行，0表示第一行
//        XSSFRow row = sheet.createRow(0);
//        //创建单元格，0表示第一个单元格
//        row.createCell(0).setCellValue("会员数据统计");
//        sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//
//        XSSFRow row1 = sheet.createRow(1);
//        row1.createCell(0).setCellValue("今日新增会员数");
//        row1.createCell(1).setCellValue((Integer)map.get("todayNewMember"));
//        row1.createCell(2).setCellValue("总会员数");
//        row1.createCell(3).setCellValue((Integer)map.get("totalMember"));
//
//        XSSFRow row2 = sheet.createRow(2);
//        row2.createCell(0).setCellValue("本周新增会员数");
//        row2.createCell(1).setCellValue((Integer)map.get("thisWeekNewMember"));
//        row2.createCell(2).setCellValue("本月新增会员数");
//        row2.createCell(3).setCellValue((Integer)map.get("thisMonthNewMember"));
//
//        XSSFRow row3 = sheet.createRow(3);
//        row3.createCell(0).setCellValue("预约到诊数据统计");
//        sheet.addMergedRegion(new CellRangeAddress(3,3,0,3));
//
//        XSSFRow row4 = sheet.createRow(4);
//        row4.createCell(0).setCellValue("今日预约数");
//        row4.createCell(1).setCellValue((Integer)map.get("todayOrderNumber"));
//        row4.createCell(2).setCellValue("今日到诊数");
//        row4.createCell(3).setCellValue((Integer)map.get("todayVisitsNumber"));
//
//        XSSFRow row5 = sheet.createRow(5);
//        row5.createCell(0).setCellValue("本周预约数");
//        row5.createCell(1).setCellValue((Integer)map.get("thisWeekOrderNumber"));
//        row5.createCell(2).setCellValue("本周到诊数");
//        row5.createCell(3).setCellValue((Integer)map.get("thisWeekVisitsNumber"));
//
//        XSSFRow row6 = sheet.createRow(6);
//        row6.createCell(0).setCellValue("本月预约数");
//        row6.createCell(1).setCellValue((Integer)map.get("thisMonthOrderNumber"));
//        row6.createCell(2).setCellValue("本月到诊数");
//        row6.createCell(3).setCellValue((Integer)map.get("thisMonthVisitsNumber"));
//
//        XSSFRow row7 = sheet.createRow(7);
//        row7.createCell(0).setCellValue("热门套餐");
//        sheet.addMergedRegion(new CellRangeAddress(7,7,0,3));
//
//        XSSFRow row8 = sheet.createRow(8);
//        row8.createCell(0).setCellValue("套餐名称");
//        row8.createCell(1).setCellValue("预约数量");
//        row8.createCell(2).setCellValue("占比");
//        row8.createCell(3).setCellValue("备注");
//
//        List<Map> mapList = (List<Map>) map.get("hotSetmeal");
//        for (int i = 0; i < mapList.size(); i++) {
//            XSSFRow rows = sheet.createRow(9+i);
//            rows.createCell(0).setCellValue(String.valueOf(mapList.get(i).get("name")));
//            rows.createCell(1).setCellValue(String.valueOf(mapList.get(i).get("count")));
//            rows.createCell(2).setCellValue(String.valueOf(mapList.get(i).get("rate")));
//            rows.createCell(3).setCellValue((String) mapList.get(i).get("remark"));
//        }
//
//        try {
//            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
//            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("report.xlsx", "UTF-8"));
//            ServletOutputStream outputStream = response.getOutputStream();
//            xssfWorkbook.write(outputStream);
//            outputStream.flush();
//            outputStream.close();
//            xssfWorkbook.close();
//            return "success";
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "导出信息出错";
//        }
//    }
}
